package com.hevs.samplewebapp.server.json;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

/**
 * Object used by Jackson to serialize JSON java.util.Date to JSON string
 * 
 * @author Arnaud Durand
 * @date 29.03.2012
 */
public class DateSerializer extends JsonSerializer<Date>{

	/**
	 * Date formatter
	 */
	static SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
	
	/**
	 * Method to serialize the Json
	 * @param value
	 * Java object to serialize
	 * @param jgen
	 * JSON generator
	 * @throws IOException, JsonProcessingException
	 * exception when the JSON isn't correct.
	 */
	@Override
	public void serialize(Date value, JsonGenerator jgen,
			SerializerProvider provider) throws IOException,
			JsonProcessingException {
	
		jgen.writeString(formatter.format(value)); //using the date object from earlier
		
	}

}
